/*
This code is meant to read in and prepare matches of PPP loans to EINs for use in all analyses
*/

//Read in data from split files that were split to improve speed

foreach rang in A_CO CT_ID IL_MI MN_NY OH_TN TX_Z {
	import delimited "$datadir/match/exact_matches_`rang'", clear
	gen match_type = 1
	save "$datadir/match/exact_matches_`rang'", replace
	
	import delimited "$datadir/match/zip_matches_`rang'", clear
	gen match_type = 2
	save "$datadir/match/zip_matches_`rang'", replace
	
	import delimited "$datadir/match/city_matches_`rang'", clear
	gen match_type = 3
	save "$datadir/match/city_matches_`rang'", replace
	
	import delimited "$datadir/match/county_matches_`rang'", clear
	gen match_type = 4
	save "$datadir/match/county_matches_`rang'", replace
	
	import delimited "$datadir/match/state_matches_`rang'", clear
	gen match_type = 5
	save "$datadir/match/state_matches_`rang'", replace
	
	import delimited "$datadir/match/k_matches_`rang'", clear
	gen match_type = 6
	save "$datadir/match/k_matches_`rang'", replace
	
	import delimited "$datadir/match/c_matches_`rang'", clear
	gen match_type = 7
	save "$datadir/match/c_matches_`rang'", replace
	
} 


//Read in and replace some schedule C information that was either duplicated or did not have the most useful Id number

use "$datadir/match/unique_c/sch_c_u", clear //Code to produce this file was redacted for disclosure reasons
gen match_type = 8

rename ein firm_tin

save "$datadir/match/unique_c/sch_c_firm", replace

//Now append all of the files together

clear
foreach rang in A_CO CT_ID IL_MI MN_NY OH_TN TX_Z {
	cap append using "$datadir/match/exact_matches_`rang'"
	cap append using "$datadir/match/zip_matches_`rang'"
	cap append using "$datadir/match/city_matches_`rang'"
	cap append using "$datadir/match/county_matches_`rang'"
	cap append using "$datadir/match/state_matches_`rang'"
	cap append using "$datadir/match/k_matches_`rang'"
	cap append using "$datadir/match/c_matches_`rang'"
} 

duplicates drop loannumber, force

//Add in the cleaned schedule C ID nos

merge 1:1 loannumber using "$datadir/match/unique_c/sch_c_firm", nogen keepusing(id firm_tin match_type) update replace

replace firm_tin = id if firm_tin == .

//Replace ID with indiv_id where firm_tin is missing (for a select group of schedule C's without a separate EIN)
replace id = indiv_id if id == .

drop if loannumber == .

save "$datadir/match/all_matches", replace
